package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.Entity.Message;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Api(tags = "订单相关操作")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private WebSocketServer webSocketServer;

    @PostMapping("/submit")
    @ApiOperation("提交订单操作")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO){

        OrderSubmitVO vo=orderService.subimitOrder(ordersSubmitDTO);

        Message message=new Message();
        message.setType(1);
        message.setOrderId(vo.getId());
        message.setContent("订单号："+vo.getOrderNumber());
        String jsonString = JSON.toJSONString(message);
        webSocketServer.sendToAllClient(jsonString);
        return Result.success(vo);
    }
    @PutMapping("/payment")
    @ApiOperation("预支付操作")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){

        OrderPaymentVO vo=orderService.payment(ordersPaymentDTO);
        return Result.success(vo);
    }
}
